package ReSofting.it.GoldDirector.Helpers;

import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;

import ReSofting.it.GoldDirector.Interfaces.IRemoteCallListener;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;

public class GetDataAsync extends AsyncTask<String, Void, List<String>> {
	 
    private final static String TAG = GetDataAsync.class.getName();
    
    IRemoteCallListener<List<String>> listener;
 
    
    public GetDataAsync(IRemoteCallListener<List<String>> listener) {
        this.listener = listener;
    }
 
    @Override
    protected List<String> doInBackground(String... urls) 
    {
    	HttpEntity entity = null;
        List<String> oggettiList = new ArrayList<String>();
        String url = urls[0];
        
        
        
        HttpHost host = new HttpHost("10.0.2.2" , 8096);
        HttpGet get = new HttpGet( "/Services/OrdersSvc.svc/GetMenu" ); //url
               

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://10.0.2.2:80/Services/OrdersSvc.svc/GetMenu" );
 
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");
               
        oggettiList.add("Uri: "+ request.getURI());
        
        try {
        	
        	
             HttpResponse response = httpClient.execute(request);             
                                       
             
             //if(response.getStatusLine().getStatusCode() == 200)
             //}
                 String responseString = EntityUtils.toString(response.getEntity());
                 JSONArray rawPersonaList = new JSONArray(responseString);
                 
                 String dato = "";
                 for(int i = 0; i < rawPersonaList.length(); i++) {
                     //"traduco" da JsonArray a Persona
                 	 oggettiList.add(rawPersonaList.getString(i));
                 }
            //}
             
             //oggettiList.add("\r\n SC:"+ response.getStatusLine());
        }
                 catch(SocketException ex)
                 {
                   Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
                     ex.printStackTrace();
                     
                     oggettiList.add("Error : "+ ex.getMessage());
                 }
             
         catch (Exception e) {
            Log.d(TAG, e.getMessage());
            
            oggettiList.add("Error : "+ e.getMessage());
            
            e.printStackTrace();
            
            
        }
       
        return oggettiList;
    }
 
    protected void onPostExecute(List<String> oggettiList) {
       listener.onRemoteCallComplete(oggettiList);
    }
 
}//fC

///////////////////




